package practice;
import java.util.ArrayList;
import java.util.List;

class Player{
    private int position;
    private  boolean isPlaying;
    private List<String> cards;
    public Player(int position){
        this.position=position;
        this.isPlaying=true;
        this.cards=new ArrayList<>();
    }
    public int getPosition(){
        return position;
    }
    public boolean isPlaying(){
        return isPlaying;
    }
    public void setPlaying(boolean playing){
        isPlaying=playing;
    }
    public List<String>getCards(){
        return cards;
    }
    public void addCard(String card){
        cards.add(card);
    }
}
class CardGame{
    private List<String> remainCards;
    private List<Player> players;
    private int currentDealerPosition;//当前庄家位置
    public CardGame(){
        remainCards=new ArrayList<>();
        players=new ArrayList<>();
        currentDealerPosition=0;//初始化庄家位置
    }
    //初始化牌
    public void initializeCards() {
        String[] suits = {"@", "#", "$", "%"}; // 花色
        String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};

        for (String suit : suits) {
            for (String rank : ranks) {
                remainCards.add(suit + rank);
            }
        }
    }
    public void initializePlayers() {//初始化玩家
        for (int i = 0; i < 5; i++) {
            players.add(new Player(i));
        }
    }
    //发牌方式
    public void dealCards(){
        if(remainCards.isEmpty()){
            System.out.println("没牌了");
            return;
        }
        //从当前位置发牌
        for(int j=0;j<players.size();j++){
            int position=(currentDealerPosition+j)%players.size();
            Player player = players.get(position);
            if (player.isPlaying()){
                String card= remainCards.remove(0);
                player.addCard(card);
                System.out.println(player.getPosition()+"获得了"+card);
            }
        }
        currentDealerPosition=(currentDealerPosition+1)%players.size();
    }
    //放弃游戏
    public void  giveUp(int position){
        for(Player player:players){
            if(player.getPosition()==position){
                player.setPlaying(false);
                System.out.println("玩家"+position+"放弃了游戏");
                break;
            }
        }
    }
    //剩余牌
    public void showRemainingCards(){
        System.out.println("剩余"+remainCards.size());
    }
    //玩家手牌
    public void showPlayerCards(){
        for (Player player:players){
            System.out.println("玩家"+player.getPosition()+"手牌"+player.getCards());
        }
    }
}
public class CardGameTest {
    public static void main(String[] args) {
        CardGame game = new CardGame();
        game.initializePlayers();
        game.initializeCards();
        game.dealCards();
        game.showPlayerCards();
        game.giveUp(2);
    }
}
